<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //字符类:[]表示有一系列的字符可供选择，只要匹配其中的一个就可以了
        var rg = /[abc]/;   //只要包含有a或者只要包含有b或者只要包含有c，都会返回true
        console.log(rg.test('andy'));  //true
        console.log(rg.test('baby')); //true
        console.log(rg.test('color')); //true
        console.log(rg.test('red'));//false
        console.log(rg.test('abc'));
        console.log('----------------------------');

        var rg1 = /^[abc]$/;//三选1，只要是a，或者是b,或者是c,这三个字母才返回true
        console.log(rg1.test('aa'));//false 
        console.log(rg1.test('a'));//true
        console.log(rg1.test('b'));//true
        console.log(rg1.test('bb'));//false
        console.log(rg1.test('c'));//true
        console.log(rg1.test('abc'));//false
        console.log('---------------------');
        var rg2 = /^[a-z]$/; //26个字母任何一个字母都返回true
        console.log(rg2.test('a')); //true
        console.log(rg2.test('z'));//true
        console.log(rg2.test(1));//false
        console.log(rg2.test('Z'));//false

        //字符组合，26个字母,0-9数字，下划线，横线无论大小写都可以写
        console.log('-----------------------');
        var reg = /^[a-zA-Z0-9_-]$/;
        console.log(reg.test('a'));//true
        console.log(reg.test('A'));//true
        console.log(reg.test(4));//true
        console.log(reg.test('-'));//true
        console.log(reg.test('_'));//true

        console.log('--------------------------');
        var reg1 = /^[^a-zA-Z0-9_-]$/;  //^取反不能选这些
        console.log(reg1.test('a'));//false
        console.log(reg1.test('A'));//false
        console.log(reg1.test('.'));//true
        console.log(reg1.test('！'));//true
    </script>
</body>

</html>